#!/usr/bin/env bash

DEFAULT_HOST="https://gitee.com/tj123/common/raw/master"
HOST="http://<%= $host %>"
if [[ "$HOST" =~ ^http:\/\/\<.*\>$ ]];then
  HOST=$DEFAULT_HOST
fi
MIRROR_URL="http://mirrors.tellfish.cn:8081"

INSTALL_DIR="/home/nexus"
FILE_URL="$HOST/mirrors/nexus/nexus-3.14.0-04-unix.tar.gz"

which java
if [ ! $? -eq 0 ]; then
  curl $HOST/mirrors/jdk/install.sh|bash
fi

which wget
if [ ! $? -eq 0 ]; then
   yum install -y wget
fi

tmp="/var/nexus_`date +%s`"
wget $FILE_URL -P $tmp
if [ ! $? -eq 0 ]; then
  echo "下载文件失败,请检查网络配置是否正确"
  exit 1
fi
file=`ls $tmp`
if [ "" == $file ]; then
  echo "下载的文件不存在！"
  exit 1
fi
useradd nexus
if [ ! -d $INSTALL_DIR ]; then
  mkdir -p $INSTALL_DIR
fi
tar -zxvf $tmp/$file -C $INSTALL_DIR
rm -rf $tmp
home=$INSTALL_DIR/`ls $INSTALL_DIR|grep nexus|awk '{print $1}'`
# groupadd nexus
# useradd nexus -c neuxs -d /home/nexus -r -g nexus -s /bin/false
chown nexus:nexus -R /home/nexus
ln -s $home/bin/nexus /usr/bin/nexus
cat << EOF >/etc/init.d/nexus
#!/bin/sh
# chkconfig:         2345 75 15
# description:       nexus
### BEGIN INIT INFO
# Provides:          nexus
# Required-Start:    \$all
# Required-Stop:     \$all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nexus
### END INIT INFO

su nexus -c "/usr/bin/nexus \$*"
EOF

chmod a+x /etc/init.d/nexus
chown nexus:nexus /etc/init.d/nexus

exit 0
